package com.zfzs.post.common.bean;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @description: response输出提示信息
 * @author: ck
 * @create: 2019-06-10 15:34
 **/
public class WriterResult {

    /**
     *输出提示信息
     * @param code 错误码
     * @param msg 错误信息
     */
    public static void writerResponse(HttpServletResponse response, String code, String msg){
        response.setHeader("Content-Type", "application/json;charset=utf-8");
        try {
            response.getWriter().write(JSON.toJSONString(ResponseResult.builder()
                    .code(code)
                    .msg(msg)
                    .data(new Object())
                    .build()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取json格式参数
     * @param request
     * @return
     */
    public static String requestAllParms(HttpServletRequest request) {
        String param= null;
        try {
            BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);

            JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());
            param= jsonObject.toJSONString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return param;
    }
}
